#include <bits/stdc++.h>
using namespace std;
unsigned long lg2[32];
void init(){
    lg2[0] = 1;
    for (int i = 1; i < 32; i++)
        lg2[i] = 1 << i;
}
int main() {
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    int n,m,t;
    init();
    cin >> n >> m;
    int prea[n],que[m];
    for (int i = 0; i < n; i++) cin >> prea[i];
    for (int i = 0; i < m; i++) cin >> que[i];
    int i;
    for (i = 31; i >= 0; i--) if (lg2[i] < n) break;
    for (int j = 0; j < i + 1; j++) cin >> t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        int x[4],ans;
        cin >> x[0] >> x[1] >> x[2] >> x[3];
        for (int j = 0; j < m; j++) {
            for (int k = 0; k < n; k++) prea[k] = prea[k] ^ x[k%4];
            ans = prea[0];
            for (int k = 1; k < n; k++) ans ^= (k + 1) * prea[k];
        }
        cout << ans << endl;
    }
    return 0;
}
